package com.fyakm.daodejing.simple.stockTest.util;

import com.fyakm.daodejing.simple.stockTest.domain.Investors;
import com.fyakm.daodejing.simple.stockTest.domain.StockEntity;
import com.fyakm.daodejing.simple.stockTest.domain.StockHistory;
import com.fyakm.daodejing.simple.stockTest.service.ProfitCalculator;
import com.fyakm.daodejing.simple.stockTest.service.TradingSimulator;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

/**
 * 股票交易工具类
 */
public class StockTradingUtils {

    /**
     * 模拟交易并计算盈利
     *
     * @param stockCode      股票代码
     * @param initialAmount  投资人初始金额
     * @param stockHistory   股票历史数据
     * @return 投资人盈利信息
     */
    public static TradingResult simulateTrading(String stockCode, Integer initialAmount, List<StockHistory> stockHistory) {
        // 初始化股票
        StockEntity stock = new StockEntity();
        stock.setCode(stockCode);
        stock.setHistory(stockHistory);

        // 初始化投资人
        Investors investor = new Investors();
        investor.setId(1);
        investor.setInitialMoney(initialAmount);
        investor.setName("投资人");
        investor.setMoney(initialAmount);
        investor.setStockMap(new HashMap<>());

        // 模拟交易
        TradingSimulator.simulateTrading(investor, stock, stockHistory);

        // 计算盈利
        double profit = ProfitCalculator.calculateProfit(investor, stock);
        double profitRate = (profit / investor.getInitialMoney()) * 100; // 盈利率

        // 返回结果
        return new TradingResult(stockCode, initialAmount, profit, profitRate);
    }

    /**
     * 交易结果类
     */
    public static class TradingResult {
        private String stockCode;      // 股票代码
        private double initialAmount;  // 初始金额
        private double profit;         // 盈利金额
        private double profitRate;     // 盈利率

        public TradingResult(String stockCode, double initialAmount, double profit, double profitRate) {
            this.stockCode = stockCode;
            this.initialAmount = initialAmount;
            this.profit = profit;
            this.profitRate = profitRate;
        }

        @Override
        public String toString() {
            DecimalFormat decimalFormat = new DecimalFormat("#,###.00");
            return "股票代码: " + stockCode +
                    ", 初始金额: " + decimalFormat.format(initialAmount) +
                    ", 盈利金额: " + decimalFormat.format(profit) +
                    ", 盈利率: " + decimalFormat.format(profitRate) + "%";
        }

        public String getStockCode() {
            return stockCode;
        }

        public void setStockCode(String stockCode) {
            this.stockCode = stockCode;
        }

        public double getInitialAmount() {
            return initialAmount;
        }

        public void setInitialAmount(double initialAmount) {
            this.initialAmount = initialAmount;
        }

        public double getProfit() {
            return profit;
        }

        public void setProfit(double profit) {
            this.profit = profit;
        }

        public double getProfitRate() {
            return profitRate;
        }

        public void setProfitRate(double profitRate) {
            this.profitRate = profitRate;
        }
    }


    // 分析盈亏情况
    public static void analyzeProfit(List<TradingResult> resultList) {
        int winCount = 0;
        int loseCount = 0;
        double totalProfit = 0;
        double totalProfitRate = 0;
        for (TradingResult result : resultList) {
            if (result.getProfit() > 0) {
                winCount++;
            } else {
                loseCount++;
            }
            totalProfit += result.getProfit();
            totalProfitRate += result.getProfitRate();
        }
        double avgProfit = totalProfit / resultList.size();
        double avgProfitRate = totalProfitRate / resultList.size();
        System.out.println("盈利次数: " + winCount);
        System.out.println("亏损次数: " + loseCount);
        System.out.println("平均盈利: " + avgProfit);
        System.out.println("平均盈利率: " + avgProfitRate + "%");
    }




}